package com.atguigu.gmall.web.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.search.SkuEsFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class SearchListController {

    @Autowired
    SkuEsFeignClient skuEsFeignClient;

    /**
     * 检索
     *
     * @param
     * @return
     */
    @GetMapping("/list.html")
    public String search(SearchParam searchParam, Model model) {

        //1、远程调用检索服务的请求
        log.info("携带的查询参数：{}", searchParam);

        //2、根据请求不同条件，让检索服务检索商品
        Map<String, Object> result = skuEsFeignClient.searchGood(searchParam);
        //trademarkList、attrsList、goodsList、total、pageSize、pageNo、totalPages
        model.addAllAttributes(result);

        model.addAttribute("searchParam", searchParam);

        //构建urlParam
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);

        //回显排序规则，orderMap type/sort
        Map<String, Object> orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap", orderMap);

        //构建面包屑
        String trade = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam",trade);

        //attrId/attrName/attrValue
        List<Map<String,Object>> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);


        return "list/index";
    }

    private List<Map<String, Object>> buildPropsParamList(SearchParam searchParam) {
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        // attrId/attrValue/attrName
        if (searchParam.getProps() != null && searchParam.getProps().length > 0){
            for (String prop : searchParam.getProps()) {
                String[] split = prop.split(":");
                HashMap<String, Object> propMap = new HashMap<>();
                propMap.put("attrId",split[0]);
                propMap.put("attrName",split[2]);
                propMap.put("attrValue",split[1]);
                list.add(propMap);
            }
        }
        return list;
    }

    private String buildTrademarkParam(SearchParam searchParam) {
        if (!StringUtils.isEmpty(searchParam.getTrademark())){
            //trademark=1:小米
            return "品牌:"+searchParam.getTrademark().split(":")[1];
        }
        return null;
    }

    private HashMap<String, Object> buildOrderMap(SearchParam searchParam) {
        String order = searchParam.getOrder();
        HashMap<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(order)) {
            String[] split = searchParam.getOrder().split(":");
            map.put("type", split[0]);
            map.put("sort", split[1]);
        }else {
            map.put("type","1");
            map.put("sort","desc");
        }
        return map;
    }

    /**
     * 根据前端传来的参数动态拼装出一个完整的url地址，方便前端下次直接使用
     *
     * @param searchParam
     * @return
     */
    private String buildUrlParam(SearchParam searchParam) {
        StringBuilder builder = new StringBuilder();
        if (!StringUtils.isEmpty(searchParam.getKeyword())) {
            builder.append("&keyword=" + searchParam.getKeyword());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory3Id()+"")) {
            builder.append("&category3Id=" + searchParam.getCategory3Id());
        }
        if (!StringUtils.isEmpty(searchParam.getTrademark())) {
            builder.append("&trademark=" + searchParam.getTrademark());
        }
        if (searchParam.getProps() != null && searchParam.getProps().length > 0) {
            for (String prop : searchParam.getProps()) {
                builder.append("&props=" + prop);
            }
        }
        //if (!StringUtils.isEmpty(searchParam.getOrder())) {
        //    builder.append("&order=" + searchParam.getOrder());
        //}
        return "list.html?"+ builder.toString();
    }
}
